home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / rekey.arc / REKEY.C next >
C/C++ Source or Header  |  1985-05-26  |  4KB  |  212 lines

  1. #include <stdio.h>
  2. #include <sgtty.h>
  3. instruct()
  4. {
  5.     static char *instructions[] =
  6.     {
  7.         "Rekey : Usage - rekey fname\n",
  8.         "Where fname is the name of the key redefinition file you\n",
  9.         "wish to create.\n\n",
  10.         "Hit the key you want to redefine, then type the string you\n",
  11.         "want the key defined as, followed by a carriage return.\n",
  12.         "Use the following escapes to insert control chars in the string:\n",
  13.         "                \\n = crlf sequence\n",
  14.         "                \\t = tab\n",
  15.         "                \\b = backspace\n",
  16.         "                \\r = carriage return\n",
  17.         "                \\f = form feed\n",
  18.         "                \\\\  = backslash\n",
  19.         "Hit the escape key to end this program\n",
  20.         "to clear all keyboard definitions\n",
  21.         "To redefine the keyboard, use the command\n",
  22.         "               type fname<cr>\n",
  23.         "where fname is the name of the file you created with rekey\n",
  24.         NULL
  25.     };
  26.     register char **instructor = instructions;
  27.     while (*instructor)
  28.     {
  29.         write(2,*instructor,strlen(*instructor));
  30.         instructor++;
  31.     }
  32. }
  33.  
  34. /*
  35. ** returns null if normal character, returns extended character otherwise.
  36. ** character read is stuffed into *c
  37. */
  38. ext_getc(c)
  39.     register int *c;
  40. {
  41.     register int returnval;
  42.     struct sgttyb gtty;
  43.     ioctl(stdin,TIOCGETP,>ty);
  44.     gtty.sg_flags |= RAW;
  45.     ioctl(stdin,TIOCSETP,>ty);
  46.     if (*c = getc(stdin))
  47.         returnval = NULL;
  48.     else
  49.         returnval = *c = getc(stdin);
  50.     if (*c == 3 || *c == 26 || *c == 27)
  51.     {
  52.         *c = EOF;
  53.         return EOF;
  54.     }
  55.     gtty.sg_flags &= (RAW ^ 0xFFFF);
  56.     ioctl(stdin,TIOCSETP,>ty);
  57.     return returnval;
  58. }
  59.  
  60. char keybuf[128];
  61. char tmpbuf[128];
  62.  
  63. map_string(to,from)
  64.     char* to;
  65.     char* from;
  66. {
  67.     while (*from)
  68.     {
  69.         if (*from == '\\')
  70.         {
  71.             switch (*++from)
  72.             {
  73.             case 'n':
  74.                 *to++ = '\r';
  75.                 *to++ = '\n';
  76.                 break;
  77.             case 't':
  78.                 *to++ = 9;
  79.                 break;
  80.             case 'b':
  81.                 *to++ = 8;
  82.                 break;
  83.             case 'r':
  84.                 *to++ = '\r';
  85.                 break;
  86.             case 'f':
  87.                 *to++ = '\f';
  88.                 break;
  89.             default:
  90.                 *to++ = *from;
  91.             }
  92.         } else
  93.         {
  94.             *to++ = *from;
  95.         }
  96.         from++;
  97.     }
  98. }
  99.  
  100. define_key(op)
  101.     FILE *op;
  102. {
  103.     int c;
  104.     char *string;
  105.     register int is_extended;
  106.     if (EOF == (is_extended = ext_getc(&c)))
  107.         return EOF;
  108.     if (is_extended)
  109.     {
  110.         if (c >= 59 && c <= 68)
  111.         {
  112.             printf("F%d =",c-58);
  113.         } else if (c >= 84 && c <= 93)
  114.         {
  115.             printf("Shift F%d =",c-83);
  116.         } else if (c >= 94 && c <= 103)
  117.         {
  118.             printf("Ctrl F%d =",c-103);
  119.         } else if (c >= 104 && c <= 113)
  120.         {
  121.             printf("Alt F%d =",c-103);
  122.         } else
  123.         {
  124.             switch (c)
  125.             {
  126.             case 71:
  127.                 printf("Home=");
  128.                 break;
  129.             case 72:
  130.                 printf("%c=",24);
  131.                 break;
  132.             case 73:
  133.                 printf("Pg Up=");
  134.                 break;
  135.             case 75:
  136.                 printf("Left Arrow="); /* cursor left */
  137.                 break;
  138.             case 77:
  139.                 printf("Right Arrow="); /* cursor right */
  140.                 break;
  141.             case 79:
  142.                 printf("End=");
  143.                 break;
  144.             case 80:
  145.                 printf("%c=",25); /* cursor down */
  146.                 break;
  147.             case 81:
  148.                 printf("Pg Dn=");
  149.                 break;
  150.             case 82:
  151.                 printf("Ins=");
  152.                 break;
  153.             case 83:
  154.                 printf("Del=");
  155.                 break;
  156.             case 119:
  157.                 printf("Ctrl Home=");
  158.                 break;
  159.             case 132:
  160.                 printf("Ctrl Pg Up=");
  161.                 break;
  162.             case 115:
  163.                 printf("Ctrl Left Arrow="); /* cursor left */
  164.                 break;
  165.             case 116:
  166.                 printf("Ctrl Right Arrow="); /* cursor right */
  167.                 break;
  168.             case 117:
  169.                 printf("Ctrl End=");
  170.                 break;
  171.             case 118:
  172.                 printf("Ctrl Pg Dn=");
  173.                 break;
  174.             default:
  175.                 break;
  176.             }
  177.         }
  178.         if (NULL == gets(tmpbuf))
  179.             return EOF;
  180.         map_string(keybuf,tmpbuf);    /* map newlines to crlfs */
  181.         /* output the redefines string to file */
  182.         fprintf(op,"\033[0;%d",c);
  183.         string = keybuf;            /* control string */
  184.         while (*string)
  185.         {
  186.             fprintf(op,";%d",*string);    /* spit each decimal character */
  187.             ++string;
  188.         }
  189.         fprintf(op,"p");                /* terminate the control string */
  190.  
  191.     }
  192. }
  193.  
  194. main(argc,argv)
  195.     int argc;
  196.     char *argv[];
  197. {
  198.     FILE *op;
  199.     if (argc == 1)
  200.         instruct();
  201.     if (!--argc)    /* if no args are left */
  202.         exit(0);
  203.     if (NULL == (op = fopen(*(++argv),"w")))
  204.     {
  205.         fprintf(stderr,"rekey : cant open %s\n",*argv);
  206.         exit(1);
  207.     }
  208.     while (EOF != define_key(op))
  209.         ;
  210.     exit(0);
  211. }
  212.